/*-------------------------------------------------------------------------*/
/**
  @file		geomview_i.c
  @author	Renaud Wanschoor N. Devillard
  @date	Sep 1998
  @version	$Revision: 2.2 $
  @brief	C interface to geomview.

  geomview is freely available, this is a C interface to plot partitions,
  this is base on N. Devillard gnuplot c interface
  
*/
/*--------------------------------------------------------------------------*/

#include <geomview_i.h>

/** Maximal size of a gnuplot command */
#define GV_CMD_SIZE     	2048
/** Maximal size of a plot title */
#define GV_TITLE_SIZE   	80
/** Maximal size of a name in the PATH */
#define GV_PATH_MAXNAMESZ       4096

static char* geomview_get_program_path(char * pname);

char* geomview_get_program_path(char * pname)
{
    int         i, j, lg;
    char    *   path;
    static char buf[GV_PATH_MAXNAMESZ];

    /* Trivial case: try in CWD */
    sprintf(buf, "./%s", pname) ;
    if (access(buf, X_OK)==0) {
        sprintf(buf, ".");
        return buf ;
    }
    /* Try out in all paths given in the PATH variable */
    buf[0] = 0;
    path = getenv("PATH") ;
    if (path!=NULL) {
        for (i=0; path[i]; ) {
            for (j=i ; (path[j]) && (path[j]!=':') ; j++);
            lg = j - i;
            strncpy(buf, path + i, lg);
            if (lg == 0) buf[lg++] = '.';
            buf[lg++] = '/';
            strcpy(buf + lg, pname);
            if (access(buf, X_OK) == 0) {
                /* Found it! */
                break ;
            }
            buf[0] = 0;
            i = j;
            if (path[i] == ':') i++ ;
        }
    } else {
		fprintf(stderr, "PATH variable not set\n");
	}
    /* If the buffer is still empty, the command was not found */
    if (buf[0] == 0) return NULL ;
    /* Otherwise truncate the command name to yield path only */
    lg = strlen(buf) - 1 ;
    while (buf[lg]!='/') {
        buf[lg]=0 ;
        lg -- ;
    }
    buf[lg] = 0;
    /*printf("%s\n",buf);*/
    return buf ;
}

geomview_ctrl * geomview_init(void){
  geomview_ctrl *  handle ;

  NOTICE((getenv("DISPLAY") == NULL,"cannot find DISPLAY variable: is it set?\n"));
  WARNING((geomview_get_program_path("geomview") == NULL,"cannot find geomview in your PATH"),NULL);
  
  handle = U_malloc(1,geomview_ctrl,"geomview_init malloc error 1\n");
  WARNING(((handle->gvcmd = popen("geomview -c -", "w")) == NULL,"error starting geomview\n"),NULL);
  return handle;
}

void geomview_close(geomview_ctrl * handle){
  geomview_cmd(handle,"(exit)");
  NOTICE((pclose(handle->gvcmd) == -1,"problem closing communication to geomview\n"));
  U_free("geomview_close free error 1\n",&handle,LEND);
}

void geomview_cmd(geomview_ctrl *  handle, char *  cmd, ...){
  va_list ap ;
  char    local_cmd[GV_CMD_SIZE];

  va_start(ap, cmd);
  vsprintf(local_cmd, cmd, ap);
  va_end(ap);

  strcat(local_cmd, "\n");
  fputs(local_cmd, handle->gvcmd) ;
  fflush(handle->gvcmd) ;
}

void geomview_load(geomview_ctrl *  handle,char *filename,int type){
  switch(type){
  case GV_COMMAND:
    geomview_cmd(handle,"(load %s command)",filename);
    break;
  case GV_GEOMETRY:
    geomview_cmd(handle,"(load %s geometry)",filename);
    break;
  case GV_CAMERA:
    geomview_cmd(handle,"(load %s camera)",filename);
    break;
  default:
    geomview_cmd(handle,"(load %s)",filename);
  }
}
